package com.tonglukuaijian.survey.jsonrpc.common;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.googlecode.jsonrpc4j.JsonRpcMethod;


public class CommonJsonRpcHttpClientUtil {

	@SuppressWarnings("unchecked")
	public static <T>T getClientProxy(Class<T> clazz, final CommonJsonRpcHttpClient client){
		System.out.println(clazz);
		return  (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
				new Class[]{clazz},
				new InvocationHandler() {
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						Object argument = args ;//!= null && args.length != 1 ? args : args[0];
						String methodName = method.getName();
						JsonRpcMethod rpcMethod = method.getAnnotation(JsonRpcMethod.class);
						if (rpcMethod != null) {
							methodName = rpcMethod.value();
						}
						return client.invoke(methodName, argument, method.getGenericReturnType());
					}
				});
	}
}
